package com.threeonemeeting.activity;

import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

import com.snda.lib.ui.MessageBox;
import com.threeonemeeting.Const;
import com.threeonemeeting.DataCenter;
import com.threeonemeeting.R;
import com.threeonemeeting.adapter.UserInfoListAdapter;
import com.threeonemeeting.db.AppDBHelper;
import com.threeonemeeting.listview.CornerListView;
import com.threeonemeeting.model.UserInfo;
import com.threeonemeeting.tab.BaseTabActivity;
import com.threeonemeeting.util.MiscUtil;

public class AccountListActivity extends BaseTabActivity implements
		OnItemClickListener {

	final static int MENU_ID_DELETE = Menu.FIRST; // 删除该帐号

	private UserInfoListAdapter mAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_account_list);
		init("账号管理");

		LinearLayout norecordLayout = (LinearLayout) findViewById(R.id.norecord_layout);
		LinearLayout listLayout = (LinearLayout) findViewById(R.id.list_layout);

		// 用户尚未登录
		if (DataCenter.getInstance().currentLoginUserInfo.nUserId == 0) {
			norecordLayout.setVisibility(View.VISIBLE);
			listLayout.setVisibility(View.GONE);

			// 立即登录
			Button btnLogin = (Button) findViewById(R.id.btn_login);
			OnClickListener loginBtnClickListener = new OnClickListener() {

				public void onClick(View v) {

					Bundle b = new Bundle();
					b.putString("jumpto", "AccountListActivity");
					startActivity("MainMoreActivity", "LoginActivity", b);
				}
			};
			if (btnLogin != null) {
				btnLogin.setOnClickListener(loginBtnClickListener);
			}

			return;
		}

		norecordLayout.setVisibility(View.GONE);
		listLayout.setVisibility(View.VISIBLE);

		// 从数据库里加载该用户的授权账户
		AppDBHelper db = DataCenter.getInstance().db;
		DataCenter.getInstance().listAccounts = db.getAccount(DataCenter
				.getInstance().mainLoginUser.nUserId);

		mAdapter = new UserInfoListAdapter(this);
		CornerListView accountList = (CornerListView) findViewById(R.id.accountList);
		accountList.setAdapter(mAdapter);
		accountList.setOnItemClickListener(this);

		mAdapter.setAccountList(DataCenter.getInstance().listAccounts.users);

		MiscUtil.setListViewHeight(accountList);

		registerForContextMenu(accountList);
	}

	public void OnClickAdd(View arg0) {
		Bundle b = new Bundle();
		b.putBoolean("add", true);
		b.putString("jumpto", "AccountListActivity");
		startActivity("MainMoreActivity", "LoginActivity", b);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View view,
			ContextMenuInfo menuInfo) {
		menu.add(0, MENU_ID_DELETE, 0, "删除该关联帐号");
	}

	private void deleteUserInfo(UserInfo uInfo) {
		int nMainUserId = DataCenter.getInstance().mainLoginUser.nUserId;
		int nCurrentUserId = DataCenter.getInstance().currentLoginUserInfo.nUserId;

		// 如果uInfo是主帐号，则提示不能删除
		if (uInfo.nUserId == nMainUserId) {
			MessageBox.showToast(getApplicationContext(), "主帐号不允许删除");
			return;
		}

		// 如果uInfo是当前使用的帐号，则提示不能删除
		if (uInfo.nUserId == nCurrentUserId) {
			MessageBox.showToast(getApplicationContext(), "该帐号正被使用，不允许删除");
			return;
		}

		AppDBHelper db = DataCenter.getInstance().getDB();
		db.deleteAccount(nMainUserId, uInfo.nUserId);

		DataCenter.getInstance().listAccounts.deleteUserInfo(uInfo.nUserId);
		mAdapter.notifyDataSetChanged();
	}

	// 菜单单击响应
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo dInfo = (AdapterContextMenuInfo) item
				.getMenuInfo();

		long nIndex = dInfo.id;
		UserInfo info = mAdapter.getItem((int) nIndex);
		if (info == null)
			return false;

		switch (item.getItemId()) {

		case MENU_ID_DELETE:
			deleteUserInfo(info);
			break;

		}

		return true;
	}

	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long id) {
		int nIndex = position;
		if (nIndex < 0)
			return;

		if (nIndex > mAdapter.getCount() - 1)
			return;

		UserInfo uInfo = mAdapter.getItem(nIndex);
		if (uInfo == null)
			return;

		UserInfo currentUserInfo = DataCenter.getInstance().currentLoginUserInfo;
		if (currentUserInfo.nUserId == uInfo.nUserId)
			return;

		DataCenter.getInstance().currentLoginUserInfo = uInfo;

		MiscUtil.setPrefer(Const.Pref.PREFS_KEY_CURRENT_USER_ID,
				String.valueOf(uInfo.nUserId));
		mAdapter.notifyDataSetChanged();

		MessageBox.showToast(this, "当前登录账户切换为" + uInfo.strNickName);
	}
}
